﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using CardLogistics.Model.PutInCard;

namespace CardLogistics.DAL.PutInCard
{
    /// <summary>
    /// 半成品/成品卡入库发卡文件_数据访问类
    /// </summary>
    public class File_Description_AreaData
    {
        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "v_File_Description", StrFields, "ID", 0);
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" ID = '" + id + "' ", "*");
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public File_Description_Area GetModel(string rId)
        {
            File_Description_Area model = new File_Description_Area();
            DataRow row = GetInfoById(rId).Rows[0];
            if (row != null)
            {
                if (row["ID"] != null)
                {
                    model.ID = row["ID"].ToString();
                }
                if (row["VERSION"] != null)
                {
                    model.VERSION = row["VERSION"].ToString();
                }
                if (row["CARDKIND"] != null && row["CARDKIND"].ToString() != "")
                {
                    model.CARDKIND = row["CARDKIND"].ToString();
                }
                if (row["BATCHID"] != null)
                {
                    model.BATCHID = row["BATCHID"].ToString();
                }
                if (row["CARDBEGINSSN"] != null)
                {
                    model.CARDBEGINSSN = row["CARDBEGINSSN"].ToString();
                }
                if (row["CARDENDSSN"] != null)
                {
                    model.CARDENDSSN = row["CARDENDSSN"].ToString();
                }
                if (row["RECNUM"] != null && row["RECNUM"].ToString() != "")
                {
                    model.RECNUM = row["RECNUM"].ToString();
                }
                if (row["PUTINNO"] != null)
                {
                    model.PUTINNO = row["PUTINNO"].ToString();
                }
                if (row["PUTINDATE"] != null && row["PUTINDATE"].ToString() != "")
                {
                    model.PUTINDATE = DateTime.Parse(row["PUTINDATE"].ToString());
                }
                if (row["CREATEUSER"] != null)
                {
                    model.CREATEUSER = row["CREATEUSER"].ToString();
                }
                if (row["CREATEDATE"] != null && row["CREATEDATE"].ToString() != "")
                {
                    model.CREATEDATE = DateTime.Parse(row["CREATEDATE"].ToString());
                }
                if (row["REMARK"] != null)
                {
                    model.REMARK = row["REMARK"].ToString();
                }
                if (row["IsSubmit"] != null)
                {
                    model.IsSubmit =int.Parse(row["IsSubmit"].ToString());
                }
                
            }
            return model;
        }
        #endregion

        #region 重载新增发卡操作
        /// <summary>
        /// 重载新增发卡操作
        /// </summary>
        /// <param name="model">发卡文件实体</param>	
        /// <param name="dt">存储子表信息</param>
        public string  Add( File_Description_Area model, DataTable dt)
        {
            string StrOutPar = string.Empty;
            int val = 0;
            string sqlConn = DbHelperSQL.ConnectionString;
            using (OracleConnection conn = new OracleConnection(sqlConn))
            {
                conn.Open();
                OracleCommand cmd = new OracleCommand();
                cmd.Connection = conn;
                OracleTransaction tx = conn.BeginTransaction();
                cmd.Transaction = tx;
                try
                {
                    OracleParameter[] parameters = {
                    new OracleParameter("varOut", OracleType.VarChar,50),
					new OracleParameter("vVERSION", OracleType.VarChar,50),
					new OracleParameter("vCARDKIND", OracleType.VarChar,10),
					new OracleParameter("vBATCHID", OracleType.VarChar,20),
					new OracleParameter("vCARDBEGINSSN", OracleType.VarChar,20),
					new OracleParameter("vCARDENDSSN", OracleType.VarChar,20),
					new OracleParameter("vRECNUM", OracleType.VarChar,20),
					new OracleParameter("vPUTINNO", OracleType.NVarChar),
					new OracleParameter("vPUTINDATE", OracleType.DateTime),
					new OracleParameter("vCREATEUSER", OracleType.VarChar,20),
					new OracleParameter("vCREATEDATE", OracleType.DateTime),
					new OracleParameter("vREMARK", OracleType.NVarChar),
                    new OracleParameter("vIssubmit", OracleType.Number)};
                    parameters[0].Direction = ParameterDirection.Output;
                    parameters[1].Value = model.VERSION;
                    parameters[2].Value = model.CARDKIND;
                    parameters[3].Value = model.BATCHID;
                    parameters[4].Value = model.CARDBEGINSSN;
                    parameters[5].Value = model.CARDENDSSN;
                    parameters[6].Value = model.RECNUM;
                    parameters[7].Value = model.PUTINNO;
                    parameters[8].Value = model.PUTINDATE;
                    parameters[9].Value = model.CREATEUSER;
                    parameters[10].Value = model.CREATEDATE;
                    parameters[11].Value = model.REMARK;
                    parameters[12].Value = model.IsSubmit;

                     StrOutPar = DbHelperSQL.RunProcedure("P_File_Description_Add", parameters, parameters[0]).ToString();
                    if (StrOutPar.Split(',')[0] == "ok")
                    {
                        string putInId = StrOutPar.Split(',')[1]; //得到主键Id

                        StringBuilder strSql = new StringBuilder();
                        foreach (DataRow dr in dt.Rows)
                        {
                            strSql = new StringBuilder();//初始化
                            strSql.Append("insert into Transaction_Data");
                            strSql.Append("(DevId,OprId,IssDate,ExpDate,CardId,CardSSN,CardCSN,CardType,ChildCardType,CardMac,FgvAmt,InitAmt,IniFlag,Reserved,PutInId)");
                            strSql.Append(" values(");
                            strSql.Append("'" + dr["DevId"] + "',");
                            strSql.Append("'" + dr["OprId"] + "',");
                            strSql.Append("'" + dr["IssDate"] + "',");
                            strSql.Append("'" + dr["ExpDate"] + "',");
                            strSql.Append("'" + dr["CardId"] + "',");
                            strSql.Append("'" + dr["CardSSN"] + "',");
                            strSql.Append("'" + dr["CardCSN"] + "',");
                            strSql.Append("'" + dr["CardType"] + "',");
                            strSql.Append("'" + dr["ChildCardType"] + "',");
                            strSql.Append("'" + dr["CardMac"] + "',");
                            strSql.Append("'" + dr["FgvAmt"] + "',");
                            strSql.Append("'" + dr["InitAmt"] + "',");
                            strSql.Append("'" + dr["IniFlag"] + "',");
                            strSql.Append("'" + dr["Reserved"] + "',");
                            strSql.Append("'" + putInId + "'");
                            strSql.Append(")");

                            if (!String.IsNullOrEmpty(strSql.ToString()))
                            {
                                cmd.CommandText = strSql.ToString();
                                val += cmd.ExecuteNonQuery();

                            }
                        }
                    }
                    tx.Commit();
                }
                catch (System.Data.OracleClient.OracleException E)
                {
                    tx.Rollback();
                    throw new Exception(E.Message);
                }
                finally
                {
                    if (conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                    }
                }
            }
            return StrOutPar;
        }
        #endregion

        #region 重载修改发卡操作
        /// <summary>
        /// 重载修改发卡操作
        /// </summary>
        /// <param name="model">发卡文件实体</param>	
        /// <param name="dt">存储子表信息</param>
        public string Update(File_Description_Area model, DataTable dt)
        {
            string StrOutPar = string.Empty;
            int val = 0;
            string sqlConn = DbHelperSQL.ConnectionString;
            using (OracleConnection conn = new OracleConnection(sqlConn))
            {
                conn.Open();
                OracleCommand cmd = new OracleCommand();
                cmd.Connection = conn;
                OracleTransaction tx = conn.BeginTransaction();
                cmd.Transaction = tx;
                try
                {
                    OracleParameter[] parameters = {
				    new OracleParameter("vID", OracleType.VarChar,50),
					new OracleParameter("vVERSION", OracleType.VarChar,50),
					new OracleParameter("vCARDKIND", OracleType.VarChar,10),
					new OracleParameter("vBATCHID", OracleType.VarChar,20),
					new OracleParameter("vCARDBEGINSSN", OracleType.VarChar,20),
					new OracleParameter("vCARDENDSSN", OracleType.VarChar,20),
					new OracleParameter("vRECNUM", OracleType.VarChar,20),
					new OracleParameter("vPUTINNO", OracleType.NVarChar),
					new OracleParameter("vPUTINDATE", OracleType.DateTime),
					new OracleParameter("vCREATEUSER", OracleType.VarChar,20),
					new OracleParameter("vCREATEDATE", OracleType.DateTime),
					new OracleParameter("vREMARK", OracleType.NVarChar),
                    new OracleParameter("vIssubmit", OracleType.Number),
                    new OracleParameter("varOut", OracleType.VarChar,50)};
                    parameters[0].Value = model.ID;
                    parameters[1].Value = model.VERSION;
                    parameters[2].Value = model.CARDKIND;
                    parameters[3].Value = model.BATCHID;
                    parameters[4].Value = model.CARDBEGINSSN;
                    parameters[5].Value = model.CARDENDSSN;
                    parameters[6].Value = model.RECNUM;
                    parameters[7].Value = model.PUTINNO;
                    parameters[8].Value = model.PUTINDATE;
                    parameters[9].Value = model.CREATEUSER;
                    parameters[10].Value = model.CREATEDATE;
                    parameters[11].Value = model.REMARK;
                    parameters[12].Value = model.IsSubmit;
                    parameters[13].Direction = ParameterDirection.Output;
                    StrOutPar = DbHelperSQL.RunProcedure("P_File_Description_Update", parameters, parameters[13]).ToString();
                    if (StrOutPar.Split(',')[0] == "ok")
                    {
                        StringBuilder strSql = new StringBuilder();

                        //修改之前删除子表信息
                        strSql = new StringBuilder();//初始化
                        strSql.Append("delete from Transaction_Data where PutInId='" + model.ID + "'");
                        cmd.CommandText = strSql.ToString();
                        val += cmd.ExecuteNonQuery();
                        //循环插入子表信息
                        foreach (DataRow dr in dt.Rows)
                        {
                            strSql = new StringBuilder();//初始化
                            strSql.Append("insert into Transaction_Data");
                            strSql.Append("(DevId,OprId,IssDate,ExpDate,CardId,CardSSN,CardCSN,CardType,ChildCardType,CardMac,FgvAmt,InitAmt,IniFlag,Reserved,PutInId)");
                            strSql.Append(" values(");
                            strSql.Append("'" + dr["DevId"] + "',");
                            strSql.Append("'" + dr["OprId"] + "',");
                            strSql.Append("'" + dr["IssDate"] + "',");
                            strSql.Append("'" + dr["ExpDate"] + "',");
                            strSql.Append("'" + dr["CardId"] + "',");
                            strSql.Append("'" + dr["CardSSN"] + "',");
                            strSql.Append("'" + dr["CardCSN"] + "',");
                            strSql.Append("'" + dr["CardType"] + "',");
                            strSql.Append("'" + dr["ChildCardType"] + "',");
                            strSql.Append("'" + dr["CardMac"] + "',");
                            strSql.Append("'" + dr["FgvAmt"] + "',");
                            strSql.Append("'" + dr["InitAmt"] + "',");
                            strSql.Append("'" + dr["IniFlag"] + "',");
                            strSql.Append("'" + dr["Reserved"] + "',");
                            strSql.Append("'" + model.ID + "'");
                            strSql.Append(")");

                            if (!String.IsNullOrEmpty(strSql.ToString()))
                            {
                                cmd.CommandText = strSql.ToString();
                                val += cmd.ExecuteNonQuery();

                            }
                        }
                    }
                    tx.Commit();
                }
                catch (System.Data.OracleClient.OracleException E)
                {
                    tx.Rollback();
                    throw new Exception(E.Message);
                }
                finally
                {
                    if (conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                    }
                }
            }
            return StrOutPar;
        }
        #endregion

        #region 新增
        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Add(File_Description_Area model)
        {

            try
            {
                OracleParameter[] parameters = {
                    new OracleParameter("varOut", OracleType.VarChar,50),
					new OracleParameter("vVERSION", OracleType.VarChar,50),
					new OracleParameter("vCARDKIND", OracleType.VarChar,10),
					new OracleParameter("vBATCHID", OracleType.VarChar,20),
					new OracleParameter("vCARDBEGINSSN", OracleType.VarChar,20),
					new OracleParameter("vCARDENDSSN", OracleType.VarChar,20),
					new OracleParameter("vRECNUM", OracleType.VarChar,20),
					new OracleParameter("vPUTINNO", OracleType.NVarChar),
					new OracleParameter("vPUTINDATE", OracleType.DateTime),
					new OracleParameter("vCREATEUSER", OracleType.VarChar,20),
					new OracleParameter("vCREATEDATE", OracleType.DateTime),
					new OracleParameter("vREMARK", OracleType.NVarChar),
                    new OracleParameter("vIssubmit", OracleType.Number)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.VERSION;
                parameters[2].Value = model.CARDKIND;
                parameters[3].Value = model.BATCHID;
                parameters[4].Value = model.CARDBEGINSSN;
                parameters[5].Value = model.CARDENDSSN;
                parameters[6].Value = model.RECNUM;
                parameters[7].Value = model.PUTINNO;
                parameters[8].Value = model.PUTINDATE;
                parameters[9].Value = model.CREATEUSER;
                parameters[10].Value = model.CREATEDATE;
                parameters[11].Value = model.REMARK;
                parameters[12].Value = model.IsSubmit;

                string StrOutPar = DbHelperSQL.RunProcedure("P_File_Description_Add", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Update(File_Description_Area model)
        {
            try
            {
                OracleParameter[] parameters = {
				    new OracleParameter("vID", OracleType.VarChar,50),
					new OracleParameter("vVERSION", OracleType.VarChar,50),
					new OracleParameter("vCARDKIND", OracleType.VarChar,10),
					new OracleParameter("vBATCHID", OracleType.VarChar,20),
					new OracleParameter("vCARDBEGINSSN", OracleType.VarChar,20),
					new OracleParameter("vCARDENDSSN", OracleType.VarChar,20),
					new OracleParameter("vRECNUM", OracleType.VarChar,20),
					new OracleParameter("vPUTINNO", OracleType.NVarChar),
					new OracleParameter("vPUTINDATE", OracleType.DateTime),
					new OracleParameter("vCREATEUSER", OracleType.VarChar,20),
					new OracleParameter("vCREATEDATE", OracleType.DateTime),
					new OracleParameter("vREMARK", OracleType.NVarChar),
                    new OracleParameter("vIssubmit", OracleType.Number),
                    new OracleParameter("varOut", OracleType.VarChar,50)};
                parameters[0].Value = model.ID;
                parameters[1].Value = model.VERSION;
                parameters[2].Value = model.CARDKIND;
                parameters[3].Value = model.BATCHID;
                parameters[4].Value = model.CARDBEGINSSN;
                parameters[5].Value = model.CARDENDSSN;
                parameters[6].Value = model.RECNUM;
                parameters[7].Value = model.PUTINNO;
                parameters[8].Value = model.PUTINDATE;
                parameters[9].Value = model.CREATEUSER;
                parameters[10].Value = model.CREATEDATE;
                parameters[11].Value = model.REMARK;
                parameters[12].Value = model.IsSubmit;
                parameters[13].Direction = ParameterDirection.Output;
                string StrOutPar = DbHelperSQL.RunProcedure("P_File_Description_Update", parameters, parameters[13]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(string id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("varOut",  OracleType.VarChar,100),
					    new OracleParameter("vid",  OracleType.VarChar,50)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string StrOutPar = DbHelperSQL.RunProcedure("P_File_Description_Delete", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
